package com.zm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zm.jdbc.AccessDb;

public class MessageServlet extends HttpServlet {
    
	private AccessDb db;
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
       String method=request.getParameter("method");
       if("send".equals(method)){
    	   forSend(request,response);
    	   
       }else if("get".equals(method)){
    	   requestMessage(request,response);
    	   
       }
		
		
		
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
	{
		
		
		
    }

	 public void forSend(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		 PrintWriter out=response.getWriter();
         String message=request.getParameter("message");
         HttpSession session=request.getSession();
         String name=(String) session.getAttribute("Account");
         Date date=new Date();
         SimpleDateFormat daFormat=new SimpleDateFormat("yyyy-MM-dd");
         SimpleDateFormat tiFormat=new SimpleDateFormat("HH:mm:ss");
         String da=daFormat.format(date);
         String ti=tiFormat.format(date);
         String sql="insert into message(fromuser,content,da,ti) values('"+name+"','"+message+"','"+da+"','"+ti+"')";
         if(db.executeUpdate(sql)){
        	 out.println("success");
          }
         else 
        	 out.println("false");
	}
	 
	 
	 public void requestMessage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		 String ti=request.getParameter("time");
		 System.out.println(ti);
		 String sql="select * from message where ti>'"+ti+"' order by ti desc limit 5";
		 ResultSet set=db.executeQuery(sql);
		 response.setContentType("text/xml");
		 response.setCharacterEncoding("utf-8");
		 PrintWriter out=response.getWriter();
		 out.println("<response>");
		 try {
			while(set.next()){
				out.println("<message>");
		
				out.println("<from>"+set.getString("fromuser")+"</from>");
				out.println("<content>"+set.getString("content")+"</content>");
			
				out.println("<time>"+set.getString("ti")+"</time>");
			
				out.println("</message>"); 
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 out.println("</response>");
		 out.flush();
		 out.close();
		 
		 
	 }
	 
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		ServletContext application =config.getServletContext();
		db=(AccessDb) application.getAttribute("accessdb");
        if(db==null){		
        	db=new AccessDb();
            application.setAttribute("accessdb",db);	
        }
		
		
	}

}
